package com.ruoyi.baseData.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.baseData.domain.*;
import com.ruoyi.baseData.domain.Process;
import com.ruoyi.baseData.domain.dto.ProceduresDto;
import com.ruoyi.baseData.domain.dto.WoProceduresDto;
import com.ruoyi.baseData.domain.vo.EquipmentConfigVo;
import com.ruoyi.baseData.domain.vo.ProceduresVo;
import com.ruoyi.baseData.domain.vo.ProcessRelVo;
import com.ruoyi.baseData.domain.vo.WoProceduresVo;
import com.ruoyi.baseData.mapper.EquipmentTreeMapper;
import com.ruoyi.baseData.mapper.ProceduresMapper;
import com.ruoyi.baseData.mapper.ProcessMapper;
import com.ruoyi.baseData.mapper.ProcessRelMapper;
import com.ruoyi.baseData.service.ProceduresService;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.sql.QueryHelp;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
*
*/
@Service
public class ProceduresServiceImpl implements ProceduresService {

    @Resource
    ProceduresMapper proceduresMapper;

    /**
     *
     */
    @Resource
    EquipmentTreeMapper equipmentTreeMapper;


    @Resource
    ProcessRelMapper processRelMapper;


    @Resource
    ProcessMapper processMapper;


    @Override
    public Page<ProceduresVo> getProceduresByPage(ProceduresDto pageEntity) {
        MPJLambdaWrapper<Procedures> wrapper = new MPJLambdaWrapper<Procedures>()
                .selectAll(Procedures.class)//查询user表全部字段
                //记的测试阶段
                .eq(Procedures::getDelFlag, 0)
                .orderByDesc(Procedures::getCreateTime);
        wrapper = QueryHelp.getPredicateEx(wrapper, Procedures.class, pageEntity);
        Page<ProceduresVo> userList = proceduresMapper.selectJoinPage(new Page<>(pageEntity.getCurrentPage(), pageEntity.getPageSize()),   ProceduresVo.class, wrapper);
        return userList;
    }

    @Override
    public void addProcedures(ProceduresDto testDto) {
        String userName = SecurityUtils.getUsername();
        MPJLambdaWrapper<Procedures> wrapper = new MPJLambdaWrapper<Procedures>()
                .eq(Procedures::getCode, testDto.getCode())
                .ne(Procedures::getDelFlag, 1);;
        Procedures material = proceduresMapper.selectOne(wrapper);
        if(null != material) {
            throw new GlobalException("该material已经存在");
        }
        Procedures test2 = new Procedures();
        BeanUtils.copyProperties(testDto, test2, new String[]{"id"});
        test2.setDelFlag(false);
        test2.setCreateBy(userName);
        test2.setCreateTime(new Date());
        proceduresMapper.insert(test2);
    }

    @Override
    public void updateProcedures(ProceduresDto testDto) {
        MPJLambdaWrapper<Procedures> wrapper = new MPJLambdaWrapper<Procedures>()
                .eq(Procedures::getCode, testDto.getCode());
        Procedures material = proceduresMapper.selectOne(wrapper);
        if(null == material) {
            throw new GlobalException("该Test不存在");
        }
        BeanUtils.copyProperties(testDto, material);
        material.setCreateBy("admin");
        material.setCreateTime(new Date());
        proceduresMapper.updateById(material);
    }

    @Override
    public void delProcedures(ProceduresDto testDto) {
        Procedures material = proceduresMapper.selectById(testDto.getId());
        if(null == material) {
            throw new GlobalException("该Test不存在");
        }
        material.setDelFlag(true);
        proceduresMapper.updateById(material);
    }

    @Override
    public WoProceduresVo getEquipAndProcessByWo(WoProceduresDto woProceduresDto) {

        WoProceduresVo woProceduresVo = new WoProceduresVo();
        ProcessRelVo processRelVo = new ProcessRelVo();
        List<EquipmentConfigVo> equipmentConfigVos = new ArrayList<>();
        /**
         * 获取所有设备
         */
        String searchValue = woProceduresDto.getWoType() + "中心";
        MPJLambdaWrapper<EquipmentTree> wrapper = new MPJLambdaWrapper<EquipmentTree>()
                .eq(EquipmentTree::getName,searchValue)
                .eq(EquipmentTree::getDelFlag, false);

        EquipmentTree fatherEquipmentTree = equipmentTreeMapper.selectOne(wrapper);
        if(null == fatherEquipmentTree) {
            throw new GlobalException("请先配置加工中心");
        }
        MPJLambdaWrapper<EquipmentTree> wrapper2 = new MPJLambdaWrapper<EquipmentTree>()
                .eq(EquipmentTree::getParentId,fatherEquipmentTree.getId())
                .eq(EquipmentTree::getDelFlag, false);
        List<EquipmentTree> equipmentTrees = equipmentTreeMapper.selectList(wrapper2);
        /**
         * 获取所有工序
         */
        String searchValue2 = woProceduresDto.getWoType() + "工艺";
        MPJLambdaWrapper<Process> wrapper1 = new MPJLambdaWrapper<Process>()
                .eq(Process::getName,searchValue2)
                .eq(Process::getDelFlag, 0);
        Process process = processMapper.selectOne(wrapper1);
        ProcessRel  processRel = new ProcessRel();
        if(null != process) {
            MPJLambdaWrapper<ProcessRel> wapper1 = new MPJLambdaWrapper<ProcessRel>()
                    .eq(ProcessRel::getProcessId,process.getId())
                    .eq(ProcessRel::getSequence, woProceduresDto.getSequence())
                    .eq(Process::getDelFlag, false);
            processRel = processRelMapper.selectOne(wapper1);
            if(null != processRel) {
                BeanUtils.copyProperties(processRel, processRelVo);
            }
        }

        for(EquipmentTree equipmentTree :equipmentTrees) {
            EquipmentConfigVo equipmentConfigVo = new EquipmentConfigVo();
            equipmentConfigVo.setId(equipmentTree.getLevelId().longValue());
            equipmentConfigVo.setName(equipmentTree.getName());
            equipmentConfigVos.add(equipmentConfigVo);
        }
        woProceduresVo.setProcessRelVo(processRelVo);
        woProceduresVo.setEquipmentConfigVoList(equipmentConfigVos);
        return woProceduresVo;
    }

    @Override
    public WoProceduresVo getNextEquipAndProcessByWo(WoProceduresDto woProceduresDto) {
        WoProceduresVo woProceduresVo = new WoProceduresVo();
        ProcessRelVo processRelVo = new ProcessRelVo();

        /**
         * 获取所有工序
         */
        //todo::需要加上物料后续改动
        String searchValue2 = woProceduresDto.getWoType() + "工艺";
        MPJLambdaWrapper<Process> wrapper1 = new MPJLambdaWrapper<Process>()
                .eq(Process::getName,searchValue2)
                .eq(Process::getDelFlag, false);
        Process process = processMapper.selectOne(wrapper1);
        ProcessRel  processRel = new ProcessRel();
        if(null != process) {
            Integer nextSequence = 0;
            if(null != woProceduresDto.getSequence()) {
                nextSequence = woProceduresDto.getSequence() + 1;
            }
            MPJLambdaWrapper<ProcessRel> wapper1 = new MPJLambdaWrapper<ProcessRel>()
                    .eq(ProcessRel::getProcessId,process.getId())
                    .eq(ProcessRel::getSequence, nextSequence)
                    .eq(Process::getDelFlag, false);
            processRel = processRelMapper.selectOne(wapper1);
            if(null != processRel) {
                BeanUtils.copyProperties(processRel, processRelVo);
                woProceduresVo.setProcessRelVo(processRelVo);
            }
        }
        return woProceduresVo;
    }
}
